package com.serviceImpl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.dao.manager.InternalInfoMapper;
import com.pojo.InternalInfo;
import com.service.InternalInfoService;
import com.utils.UUIDUtils;
import com.utils.UploadUtils;

@Service
public class InternalInfoServiceImp implements InternalInfoService {

	@Resource
	private InternalInfoMapper internalInfoMapper;
	@Value("${img.upload.path}")
	private String imgUploadPath;
	@Value("${img.access.path}")
	private String imgAccessPath;

	@Override
	public int addInternalInfo(Map<String, String> paramsMap,MultipartFile[] dlimg, MultipartFile[] pxtimg,
			MultipartFile[] nbimg) {
		try {
			String uuid = UUIDUtils.getUUID();
			List<String> dlimgPath = UploadUtils.uploadQiniuImg(dlimg, imgUploadPath, imgAccessPath);
			List<String> pxtimgPath = UploadUtils.uploadQiniuImg(pxtimg, imgUploadPath, imgAccessPath);
			List<String> nbimgPath = UploadUtils.uploadQiniuImg(nbimg, imgUploadPath, imgAccessPath);
			InternalInfo entity = new InternalInfo();
			entity.setUuid(uuid);
			entity.setInternalid(Integer.valueOf(paramsMap.get("internalid")));
			entity.setName(paramsMap.get("name"));
			entity.setContent(paramsMap.get("content"));
			entity.setDlcode(paramsMap.get("dlcode"));
			entity.setPxtcode(paramsMap.get("pxtcode"));
			entity.setNbcode(paramsMap.get("nbcode"));
			entity.setCreatetime(new Date());
			if (dlimgPath != null && !dlimgPath.isEmpty()) {
				entity.setDlimg(dlimgPath.get(0));
			} 
			if (pxtimgPath != null && !pxtimgPath.isEmpty()) {
				entity.setPxtimg(pxtimgPath.get(0));
			} 
			if (nbimgPath != null && !nbimgPath.isEmpty()) {
				entity.setNbimg(nbimgPath.get(0));
			} 
			internalInfoMapper.insertSelective(entity);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public Map<String, String> getInternalInfo(Map<String, String> paramsMap) {
		return internalInfoMapper.getInternalInfo(paramsMap.get("cuuid"));
	}

	@Override
	public List<Map<String, String>> getInternalInfoByName(Map<String, String> paramsMap) {
		String page = paramsMap.get("page");
		if (page == null || "".equals(page)) {
			page = "1";
		}
		String num = paramsMap.get("num");
		if (num == null || "".equals(num)) {
			num = "10";
			paramsMap.put("num", num);
		}
		int start = (Integer.parseInt(page) - 1) * Integer.parseInt(num);
		paramsMap.put("start", start + "");
		return internalInfoMapper.getInternalInfoByName(paramsMap);
	}

	@Override
	public int updateInternalInfo(Map<String, String> paramsMap,MultipartFile[] dlimg, MultipartFile[] pxtimg,
			MultipartFile[] nbimg) {
		try {
			String uuid = paramsMap.get("uuid");
			List<String> dlimgPath = UploadUtils.uploadQiniuImg(dlimg, imgUploadPath, imgAccessPath);
			List<String> pxtimgPath = UploadUtils.uploadQiniuImg(pxtimg, imgUploadPath, imgAccessPath);
			List<String> nbimgPath = UploadUtils.uploadQiniuImg(nbimg, imgUploadPath, imgAccessPath);
			InternalInfo entity = new InternalInfo();
			entity.setUuid(uuid);
			entity.setName(paramsMap.get("name"));
			entity.setContent(paramsMap.get("content"));
			entity.setDlcode(paramsMap.get("dlcode"));
			entity.setPxtcode(paramsMap.get("pxtcode"));
			entity.setNbcode(paramsMap.get("nbcode"));
			entity.setCreatetime(new Date());
			if (dlimgPath != null && !dlimgPath.isEmpty()) {
				entity.setDlimg(dlimgPath.get(0));
			} 
			if (pxtimgPath != null && !pxtimgPath.isEmpty()) {
				entity.setPxtimg(pxtimgPath.get(0));
			} 
			if (nbimgPath != null && !nbimgPath.isEmpty()) {
				entity.setNbimg(nbimgPath.get(0));
			} 
			internalInfoMapper.updateByPrimaryKeySelective(entity);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int deleteInternalInfo(Map<String, String> paramsMap) {
		try {
			return internalInfoMapper.deleteInternalInfo(paramsMap);
		} catch (Exception e) {
			e.printStackTrace();
			return -2;
		}
	}

	@Override
	public List<Map<String, String>> getAllInterInfo(Map<String, String> paramsMap) {
		return internalInfoMapper.getAllInterInfoByName(paramsMap);
	}

}
